package com.px.project.module.ml.service.impl;

import java.util.Date;
import java.util.List;

import com.px.project.module.ml.service.IMlService;
import com.px.project.module.ml.service.IOutboundService;
import com.px.project.module.ml.service.IStockService;
import com.px.project.module.ml.service.IStorageService;
import com.px.project.module.ml.service.ITeamTakeStockService;
import com.px.project.module.ml.service.IVerificationSheetService;
import com.px.project.module.ml.vo.ml.MlCountVo;
import com.px.project.module.ml.vo.ml.MlInfoCountVo;
import com.px.project.module.ml.vo.ml.MlVerificationSheetCountVo;
import com.px.project.module.ml.vo.ml.MlWarningCountVo;
import org.springframework.stereotype.Service;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import lombok.RequiredArgsConstructor;

/**
 * 物资系统》物资看板业务逻辑实现
 *
 * @author 品讯科技
 * @date 2024-08
 */
@Service
@RequiredArgsConstructor
public class MlServiceImpl implements IMlService {
    private final ITeamTakeStockService teamTakeStockService;

    private final IStorageService storageService;

    private final IOutboundService outboundService;

    private final IStockService stockService;

    private final IVerificationSheetService verificationSheetService;

    @Override
    public MlCountVo listOfCount() {
        MlCountVo count = new MlCountVo();
        count.setMonthCount(this.teamTakeStockService.getMonthCount(DateUtil.beginOfMonth(DateUtil.date()), DateUtil.endOfMonth(DateUtil.date())));
        count.setLastMonthCount(
            this.teamTakeStockService.getMonthCount(DateUtil.beginOfMonth(DateUtil.lastMonth()), DateUtil.endOfMonth(DateUtil.lastMonth())));

        count.setToDayStorageCount(this.storageService.getStorageCount(DateUtil.parse(DateUtil.today()), DateUtil.date()));
        count.setYesterdayStorageCount(this.storageService
            .getStorageCount(DateUtil.offset(DateUtil.parse(DateUtil.today()), DateField.DAY_OF_MONTH, -1), DateUtil.parse(DateUtil.today())));

        count.setToDayOutboundCount(this.outboundService.getOutboundCount(DateUtil.parse(DateUtil.today()), DateUtil.date()));
        count.setYesterdayOutboundCount(this.outboundService
            .getOutboundCount(DateUtil.offset(DateUtil.parse(DateUtil.today()), DateField.DAY_OF_MONTH, -1), DateUtil.parse(DateUtil.today())));

        count.setStorages(
            this.storageService.findOfList(DateUtil.offset(DateUtil.parse(DateUtil.today()), DateField.DAY_OF_MONTH, -15), DateUtil.date()));

        count.setOutbounds(
            this.outboundService.findOfList(DateUtil.offset(DateUtil.parse(DateUtil.today()), DateField.DAY_OF_MONTH, -15), DateUtil.date()));
        return count;
    }

    @Override
    public List<MlWarningCountVo> listOfCountAndWarning(Long materialId) {
        return this.stockService.listOfCountAndWarning(materialId);
    }

    @Override
    public List<MlInfoCountVo> listOfCountAndInfo(Long storeId, Long materialId) {
        return this.stockService.listOfCountAndInfo(storeId, materialId);
    }

    @Override
    public List<MlVerificationSheetCountVo> listOfCountAndVerificationSheet(Long materialId, Date date) {
        Date lastDate = DateUtil.offset(date, DateField.MONTH, 1);
        DateTime beginDate = DateUtil.beginOfMonth(lastDate);
        DateTime endDate = DateUtil.endOfMonth(lastDate);
        return this.verificationSheetService.listOfCountAndVerificationSheet(materialId, beginDate, endDate);
    }
}